[scala][http4s] fix escaping of reserved words for correct model property names#21518
Conversation
d3de2ae to
de9ff5a
Compare
|
thanks for the pr can you please review the CI build failure when you've time? |
7153a80 to
738f4d0
Compare
| } | ||
| }; | ||
|
|
||
| return compiler.withEscaper(SCALA); |
There was a problem hiding this comment.
instead of overriding processCompiler, what about adding lambda function to additional properties?
There was a problem hiding this comment.
I created
private static class BacktickLambda extends AbstractScalaCodegen.CustomLambda {
@Override
public String formatFragment(String fragment) {
if (fragment.startsWith("`") && fragment.endsWith("`")) {
String unescaped = fragment.substring(1, fragment.length() - 1);
return "`" + Escapers.HTML.escape(unescaped) + "`";
}
return Escapers.HTML.escape(fragment);
}
}
added it to the properties
additionalProperties.put("fnBacktick", new BacktickLambda());
After that, I used it in the templates
{{#fnBacktick}}{{paramName}}{{/fnBacktick}}
and I ended up with non-compiling code
case class User(
`type`: String
)
There was a problem hiding this comment.
I found solution about overriding processCompiler here
There was a problem hiding this comment.
RE:
{{#fnBacktick}}{{paramName}}{{/fnBacktick}}
what about using the following instead to use the original value instead of HTML-escaped value?
{{#fnBacktick}}{{{paramName}}}{{/fnBacktick}}
There was a problem hiding this comment.
looks good
i'll merge it over the weekend if no one else has any question/comment.
have a nice weekend
There was a problem hiding this comment.
thanks. have a great weekend too
738f4d0 to
e60c51a
Compare
There is an example of correct openAPI specification:
{ "openapi": "3.0.0", "info": { "title": "Simple API", "description": "A simple API with GET and POST endpoints", "version": "1.0.0" }, "servers": [ { "url": "https://api.example.com/v1" } ], "paths": { "/users": { "get": { "summary": "Method that has models with a reserved word", "parameters": [ { "name": "typeName", "in": "query", "description": "param", "required": true, "schema": { "type": "string", "enum": [ "active", "inactive", "pending" ] } } ], "responses": { "200": { "description": "Successful response", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/User" } } } } } } } }, "components": { "schemas": { "User": { "type": "object", "properties": { "type": { "type": "string", "enum": [ "active", "inactive", "pending" ] } }, "required": [ "type" ] } } } }JSON response is generated for this OpenAPI specification:
{"_type":"active"}This is incorrect since we specified that the field should be named
type, not_type.I changed the escaping of reserved words to standard Scala backticks notation so that the JSON decoder could properly map the domain to JSON.
{"type":"active"}PR checklist
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)Hello! 👋
I'd love to get your eyes on this PR when you have a moment.
Thanks in advance!
@clasnake (2017/07), @shijinkui (2018/01), @ramzimaalej (2018/03), @chameleon82 (2020/03), @Bouillie (2020/04) @Fish86 (2023/06)